package com.combetter.changxing.base;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toolbar;

import com.combetter.changxing.R;
import com.combetter.changxing.utils.ScreenUtil;

import butterknife.ButterKnife;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

public abstract class BaseActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private Fragment showFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutId());
        ButterKnife.bind(this);
        fragmentManager = null;
        fragmentManager = getSupportFragmentManager();
        initViews();
        loadData();
        //开启沉浸式状态栏
        if (isOpenStatus()) {
            //沉浸式状态栏（透明状态栏） -- android4.4以后提供
            Window window = getWindow();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.0以上
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//让子控件填充整个屏幕
                window.setStatusBarColor(Color.TRANSPARENT);//把状态栏设置为透明
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //4.4以上
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }

            //找到指定的控件，设置paddingtop为状态栏的高度（如果找到一个id为actionbar的控件，则对它他设置paddingtop为状态栏的高度）
            Toolbar view =findViewById(R.id.actionbar);
            if (view != null) {
                int statusHeight = ScreenUtil.getStatusHeight(this);
                if (statusHeight != -1) {
                    view.setPadding(0, statusHeight, 0, 0);
                }
            }

        }
    }

    /**
     * 是否开启沉浸式状态栏
     *
     * @return
     */
    protected boolean isOpenStatus() {
        return false;
    }

    /**
     * 返回布局id
     */
    protected abstract int layoutId();

    /**
     * 获取上个页面传过来的数据
     */
    protected void getBundleData(Bundle bundle) {
    }

    /**
     * 初始化标题栏，所有标题栏相关的代码都写在这里
     */
    protected void initTitlebar() {
    }

    /**
     * 初始化View（设置控件样式、初始值等）
     */
    protected abstract void initViews();

    /**
     * 设置各种事件监听
     */
    protected void setListeners() {
    }

    /**
     * 获取数据
     */
    protected abstract void loadData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getManager().popActivity(this);
    }

    /**
     * 启动Activity
     */
    protected void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     * 启动Activity,带Bundle参数
     */
    protected void startActivity(Class<? extends Activity> cls, @NonNull Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 启动Activity,带请求码
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动Activity,带请求码,带Bundle参数
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode, @NonNull Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    protected void showFragment(int frameLayoutId, Fragment fragment) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //检测当前是否有显示的fragment，如果有则隐藏
        if (showFragment != null) {
            fragmentTransaction.hide(showFragment);
        }

        //判断需要显示的fragment是否原来出来过，如果有，则显示
        Fragment mfragment = fragmentManager.findFragmentByTag(fragment.getClass().getName());
        if (mfragment != null) {
            fragmentTransaction.show(mfragment);
        } else {
            //如果没有则add
            mfragment = fragment;
            fragmentTransaction.add(frameLayoutId, mfragment, fragment.getClass().getName());
        }
        showFragment = mfragment;
        fragmentTransaction.commit();

    }
}
